package com.example.attendance_back.service;

import com.example.attendance_back.mapper.StudentMapper;
import com.example.attendance_back.model.ResponseMessage;
import com.example.attendance_back.model.Student;
import com.example.attendance_back.utils.JWTUtils;
import io.netty.util.internal.StringUtil;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.relational.core.sql.In;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@CacheConfig(cacheNames = "student")
public class StudentService {


    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private StudentMapper mapper;


    @Autowired
    private ResponseMessage responseMessage;

    @Cacheable(value = "list")
    public ResponseMessage getAll() {

        responseMessage.setSuccess(true);
        responseMessage.setData(mapper.selectAll());

        return responseMessage;
    }


    public ResponseMessage login(Student student) {


        responseMessage.setSuccess(mapper.exits(student) > 0 ? true : false);

       String token = JWTUtils.generateToken(student.getUsername());
       responseMessage.setToken(token);

        return responseMessage;
    }


    @Cacheable(key = "#studentId")
    public Student getStudentById(Integer studentId) {
        return mapper.selectById(studentId);
    }

    @CachePut(value = "add")
    public Boolean addStudent(Student student) {
        return mapper.insert(student) > 0 ? true : false;
    }


    @CacheEvict(value = "list")
    public void removeAll() {

    }
}
